/*
1.编写设备层代码												dev-led.c			
定义平台设备对象												
		创建  初始化										
注册平台设备对象												


*/


#include <linux/module.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include "led.h"

//定义资源对象 包括寄存器地址,中断号
static struct resource led_resources[] = {
	[0] = {
		.start	= CRU_CLKGATE_CON31,
		.end	= CRU_CLKGATE_CON31 + SIZE_4 - 1,
		.flags	= IORESOURCE_MEM, //地址类型
	},
	[1] = {
		.start	= GRF_GPIO2A_IOMUX,
		.end	= GRF_GPIO2A_IOMUX + SIZE_4 - 1,
		.flags	= IORESOURCE_MEM, //地址类型
	},
	
	[2] = {
		.start	= GPIO2_SWPORTA_DDR,
		.end	= GPIO2_SWPORTA_DDR + SIZE_4 - 1,
		.flags	= IORESOURCE_MEM, //地址类型
	},
	[3] = {
		.start	= GPIO2_SWPORTA_DR,
		.end	= GPIO2_SWPORTA_DR + SIZE_4 - 1,
		.flags	= IORESOURCE_MEM, //地址类型
	},
	[4] = {
		.start	= 75,
		.end	= 75,
		.flags	= IORESOURCE_IRQ, //中断类型
	},
};


//创建3个引脚对象的数组 ，定义一个结构体用于描述硬件
static struct rk_gpio_led rk_leds[] = {
	[0] = {
		.gpio = 55,
		.desc = "gpio_led2_green",
	},
	[1] = {
		.gpio = 56,
		.desc = "gpio_led2_orange",
	},
	[2] = {
		.gpio = 13,
		.desc = "gpio_led1",
	},

};

//创建自定义数据对象
static struct leds_platform_data rk_leds_platform_data = {
	.leds = rk_leds,
	.nleds = ARRAY_SIZE(rk_leds),
};

void led_release(struct device *dev)
{
	printk("<kernel> call %s() @ %d\n", __FUNCTION__, __LINE__); 

}

//定义平台设备对象	
static struct platform_device rk_led_device = {
	.name		= "rk3399_led", //平台设备的字符串名称,也是匹配的条件
	.id		= -1,
	.dev = {
		.release = led_release, //不定义会oops
		.platform_data = &rk_leds_platform_data,
	},
	.resource	= led_resources, //指向资源数组
	.num_resources	= ARRAY_SIZE(led_resources), //资源数据
};




static int __init device_mod_init(void)
{
	printk("<kernel> call %s() @ %d\n", __FUNCTION__, __LINE__); 
	return platform_device_register(&rk_led_device);
}


static void __exit device_mod_exit(void)
{
	printk("<kernel> call %s() @ %d\n", __FUNCTION__, __LINE__); 
	return platform_device_unregister(&rk_led_device);
	
}


module_init(device_mod_init); 
module_exit(device_mod_exit); 
MODULE_LICENSE("GPL"); 


